<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>
  <script src="../vue.js"></script>
</head>
<body>
  <div id="root">
    <h1>{{title}}</h1>
    <div>{{sex}}</div>
  </div>
  <script type="module">
    Vue.config.productionTip = false

    var vm = new Vue({
      el: '#root',

      data: {
        title: 'hello',
        gender: 0,
        sex: ''
      },

      methods: {
        g2() {
          console.log('methods')
          return this.gender === 0 ? '男' : '女'
        }
      },

      // 计算属性
      // 根据依赖缓存
      computed: {
        g() {
          console.log('computed')
          // 异步是不能在计算属性里使用
          // fetch('./data.json')
          //   .then(r => r.json())
          //   .then(result => {
          //     console.log(result.x)
          //     return this.gender === result.x ? '男' : '女'
          //   })
            
          return this.gender === 0 ? '男' : '女'
        }
      },

      mounted() {
        // console.log(this)
        setTimeout(() => {
          this.gender = 1
          this.title = 'world'
        }, 2000)
      },

      watch: {
        gender: {
          immediate: true,
          handler(newValue) {
            fetch('./data.json')
              .then(r => r.json())
              .then(result => {
                let str = newValue === 0 ? '男' : '女'
                this.sex = str + result.x
              })
          }
        }
      }
    })
    

    // let json = '{"x": 100, "y": true}'
    // let bool = JSON.parse('false')
    // // let bool = x === 'false' ? false : true
    // if(bool) {
    //   console.log(0)
    // } else {
    //   console.log(1)
    // }
    // console.log(JSON.parse(undefined))
  </script>
</body>
</html>